﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Docking;

namespace BYManageInfoSystem
{
    public partial class Frm_RecvFacFee_New : DockContent
    {
        private DGBY.BLL.FactoryDataTab m_bllFactoryDataTab = new DGBY.BLL.FactoryDataTab();
        private DGBY.BLL.PaidanRegisterTab m_bllPaidanRegisterTab = new DGBY.BLL.PaidanRegisterTab();
        private DGBY.BLL.FacFeeTab m_bllFacFeeTab = new DGBY.BLL.FacFeeTab();
        private DGBY.BLL.TCCompDataTab m_bllTCCompDataTab = new DGBY.BLL.TCCompDataTab();
        private DGBY.BLL.HKTransCompDataTab m_bllHKTransCompDataTab = new DGBY.BLL.HKTransCompDataTab();
        private DGBY.BLL.FacFeeRelationTab m_bllFacFeeRelationTab = new DGBY.BLL.FacFeeRelationTab();

        private DGBY.BLL.DZCompDataTab m_bllDZCompDataTab = new DGBY.BLL.DZCompDataTab();

        public Frm_RecvFacFee_New()
        {
            InitializeComponent();
        }

        private void InitCombox()
        {
            DataSet ds = m_bllFactoryDataTab.GetList("");
            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                this.comboBoxOrderCustom.DataSource = ds.Tables[0];

                this.comboBoxOrderCustom.ValueMember = "FactoryDataTabID";
                this.comboBoxOrderCustom.DisplayMember = "工厂简称";
                this.comboBoxOrderCustom.AutoCompleteSource = AutoCompleteSource.ListItems;
                this.comboBoxOrderCustom.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
                this.comboBoxOrderCustom.SelectedIndex = 0;
            }

            //ds = m_bllPaidanRegisterTab.GetAllPaiDanList("");
            //if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            //{
            //    this.comboBoxContractNo.DataSource = ds.Tables[0];
            //    this.comboBoxContractNo.ValueMember = "PaidanRegisterTabID";
            //    this.comboBoxContractNo.DisplayMember = "合同编号";
            //    this.comboBoxContractNo.AutoCompleteSource = AutoCompleteSource.ListItems;
            //    this.comboBoxContractNo.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
            //    this.comboBoxContractNo.SelectedIndex = 0;
            //    //this.comboBoxContractNo.SelectedValue = Frm_Main.m_nContractSelectValue;
            //}

            ds = m_bllTCCompDataTab.GetList("");
            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                this.comboBoxDLTransComp.DataSource = ds.Tables[0];
                this.comboBoxDLTransComp.ValueMember = "TCCompDataTabID";
                this.comboBoxDLTransComp.DisplayMember = "拖车公司简称";
                this.comboBoxDLTransComp.AutoCompleteSource = AutoCompleteSource.ListItems;
                this.comboBoxDLTransComp.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
                this.comboBoxDLTransComp.SelectedIndex = 0;
                //this.comboBoxContractNo.SelectedValue = Frm_Main.m_nContractSelectValue;
            }

            ds = m_bllDZCompDataTab.GetList("");
            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                this.comboBoxDZComp.DataSource = ds.Tables[0];
                this.comboBoxDZComp.ValueMember = "DZCompDataTabID";
                this.comboBoxDZComp.DisplayMember = "公司简称";
                this.comboBoxDZComp.AutoCompleteSource = AutoCompleteSource.ListItems;
                this.comboBoxDZComp.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
                this.comboBoxDZComp.SelectedIndex = 0;
                //this.comboBoxContractNo.SelectedValue = Frm_Main.m_nContractSelectValue;
            }

            ds = m_bllHKTransCompDataTab.GetList("");
            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                this.comboBoxHKTransComp.DataSource = ds.Tables[0];
                this.comboBoxHKTransComp.ValueMember = "HKTransCompDataTabID";
                this.comboBoxHKTransComp.DisplayMember = "香港运输公司名称";
                this.comboBoxHKTransComp.AutoCompleteSource = AutoCompleteSource.ListItems;
                this.comboBoxHKTransComp.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
                this.comboBoxHKTransComp.SelectedIndex = 0;
                //this.comboBoxContractNo.SelectedValue = Frm_Main.m_nContractSelectValue;
            }
            
        }

        private void Frm_RecvFacFee_New_Load(object sender, EventArgs e)
        {
            InitCombox();
        }

        private void comboBoxOrderCustom_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                string str = this.comboBoxOrderCustom.SelectedValue.ToString();
                int nFacID = Convert.ToInt32(str, 10);
                DataSet ds = m_bllPaidanRegisterTab.GetListEx2("FactoryDataTab.FactoryDataTabID = '" + nFacID.ToString() + "'");
                if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                {
                    this.comboBoxContractNo.DataSource = ds.Tables[0];
                    this.comboBoxContractNo.ValueMember = "PaidanRegisterTabID";
                    this.comboBoxContractNo.DisplayMember = "合同编号";
                    this.comboBoxContractNo.AutoCompleteSource = AutoCompleteSource.ListItems;
                    this.comboBoxContractNo.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
                    //this.comboBoxContractNo.SelectedValue = Frm_Main.m_nContractSelectValue;
                }


            }
            catch { }



        }

        private void groupBox5_Enter(object sender, EventArgs e)
        {

        }

        private void comboBoxXGYSGS_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void buttonModify_Click(object sender, EventArgs e)
        {
            try
            {
                if (ClassInputValid.IsDecimal(this.errorProvider1, this.textBoxDLCWRecv))
                    return;
                if (ClassInputValid.IsDecimal(this.errorProvider1, this.textBoxDLCWPay))
                    return;
                if (ClassInputValid.IsDecimal(this.errorProvider1, this.textBoxHKCWRecv))
                    return;
                if (ClassInputValid.IsDecimal(this.errorProvider1, this.textBoxHKCWPay))
                    return;
                if (ClassInputValid.IsDecimal(this.errorProvider1, this.textBoxHKYSShouldRecv))
                    return;
                if (ClassInputValid.IsDecimal(this.errorProvider1, this.textBoxHKYSShouldPay))
                    return;
                if (ClassInputValid.IsDecimal(this.errorProvider1, this.textBoxBGFeeShouldPay))
                    return;
                if (ClassInputValid.IsDecimal(this.errorProvider1, this.textBoxSJJinE))
                    return;
                if (ClassInputValid.IsDecimal(this.errorProvider1, this.textBoxShouldPaySJ))
                    return;
                if (ClassInputValid.IsNum(this.errorProvider1, this.textBoxShouldRecvSJ))
                    return;

                if (ClassInputValid.IsDecimal(this.errorProvider1, this.textBoxShouldPayTransFee))
                    return;
                if (ClassInputValid.IsDecimal(this.errorProvider1, this.textBoxBGFeeShouldPay))
                    return;
                if (ClassInputValid.IsDecimal(this.errorProvider1, this.textBoxDZPay))
                    return;
                if (ClassInputValid.IsDecimal(this.errorProvider1, this.textBoxDHLPay))
                    return;
                if (ClassInputValid.IsNum(this.errorProvider1, this.textBoxDHLRecv))
                    return;

                if (ClassInputValid.IsDecimal(this.errorProvider1, this.textBoxZXZPay))
                    return;
                if (ClassInputValid.IsDecimal(this.errorProvider1, this.textBoxZXZRecv))
                    return;
                if (ClassInputValid.IsDecimal(this.errorProvider1, this.textBoxJCPay))
                    return;
                if (ClassInputValid.IsNum(this.errorProvider1, this.textBoxJCRecv))
                    return;


                string str = this.comboBoxContractNo.SelectedValue.ToString();
                int nPaiDanID = Convert.ToInt32(str, 10);
                DataSet ds = m_bllFacFeeTab.GetList("FacFeeTab.PaidanRegisterTabID = '" + nPaiDanID + "'");
                if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                {
                    int nFacFeeTabID = (int)ds.Tables[0].Rows[0]["FacFeeTabID"];
                    DGBY.Model.FacFeeTab modelFacFeeTab = m_bllFacFeeTab.GetModel(nFacFeeTabID);
                    modelFacFeeTab.ZGZQName = this.textBoxZGZQ.Text;
                    modelFacFeeTab.TCCompDataTabID = Convert.ToInt32(this.comboBoxDLTransComp.SelectedValue.ToString(), 10);
                    modelFacFeeTab.DZCompDataTabID = Convert.ToInt32(this.comboBoxDZComp.SelectedValue.ToString(), 10);
                    modelFacFeeTab.HKShipCompDataTabID = 1;
                    modelFacFeeTab.HKTransCompDataTabID = Convert.ToInt32(this.comboBoxHKTransComp.SelectedValue.ToString(), 10);
                    modelFacFeeTab.DHLDataTabID = 1;

                    modelFacFeeTab.ShipDLRecvMoney = Convert.ToDecimal(this.textBoxDLCWRecv.Text);
                    modelFacFeeTab.ShipDLPayMoney = Convert.ToDecimal(this.textBoxDLCWPay.Text);
                    modelFacFeeTab.ShipHKRecvMoney = Convert.ToDecimal(this.textBoxHKCWRecv.Text);
                    modelFacFeeTab.ShipHKPayMoney = Convert.ToDecimal(this.textBoxHKCWPay.Text);
                    modelFacFeeTab.TransHKRecvMoney = Convert.ToDecimal(this.textBoxHKYSShouldRecv.Text);

                    modelFacFeeTab.TransHKPayMoney = Convert.ToDecimal(this.textBoxHKYSShouldPay.Text);
                    modelFacFeeTab.SJJinE = Convert.ToDecimal(this.textBoxSJJinE.Text);
                    modelFacFeeTab.ShouldPaySJJinE = Convert.ToDecimal(this.textBoxShouldPaySJ.Text);
                    modelFacFeeTab.ShouldRecvSJJinE = Convert.ToDecimal(this.textBoxShouldRecvSJ.Text);

                    modelFacFeeTab.ShouldPayTransFee = Convert.ToDecimal(this.textBoxShouldPayTransFee.Text);
                    modelFacFeeTab.ShouldPayBGFee = Convert.ToDecimal(this.textBoxBGFeeShouldPay.Text);
                    modelFacFeeTab.ShouldPayDZFee = Convert.ToDecimal(this.textBoxDZPay.Text);

                    modelFacFeeTab.ShouldPayDHLFee = Convert.ToDecimal(this.textBoxDHLPay.Text);
                    modelFacFeeTab.ShouldRecvDHLFee = Convert.ToDecimal(this.textBoxDHLRecv.Text);
                    modelFacFeeTab.ShouldPayZXZFee = Convert.ToDecimal(this.textBoxZXZPay.Text);
                    modelFacFeeTab.ShouldRecvZXZFee = Convert.ToDecimal(this.textBoxZXZRecv.Text);
                    modelFacFeeTab.ShouldPayJCFee = Convert.ToDecimal(this.textBoxJCPay.Text);
                    modelFacFeeTab.ShouldRecvJCFee = Convert.ToDecimal(this.textBoxJCRecv.Text);

                    m_bllFacFeeTab.Update(modelFacFeeTab);
                    MessageBox.Show("数据修改成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

                }
                else
                {
                    MessageBox.Show("数据库不存在该合同费用记录!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

                }


            }
            catch { }
        }

        private void CallFeeDetail(string strFeeCate)
        {
            string str = this.comboBoxContractNo.SelectedValue.ToString();
            int nPaiDanID = Convert.ToInt32(str, 10);
            DataSet ds = m_bllFacFeeTab.GetList("FacFeeTab.PaidanRegisterTabID = '" + nPaiDanID + "'");
            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                int nFacFeeTabID = (int)ds.Tables[0].Rows[0]["FacFeeTabID"];
                Frm_FacFeeRelation frm_FacFeeRelation = new Frm_FacFeeRelation(nFacFeeTabID, strFeeCate, this.comboBoxContractNo.Text);
                frm_FacFeeRelation.StartPosition = FormStartPosition.CenterScreen;
                frm_FacFeeRelation.ShowDialog();
            }
        }
        private void buttonDLYSDetail_Click(object sender, EventArgs e)
        {
            CallFeeDetail("大陆运输");
        }

        private void buttonBGDetail_Click(object sender, EventArgs e)
        {
            CallFeeDetail("报关");
        }

        private void buttonDZDetail_Click(object sender, EventArgs e)
        {
            CallFeeDetail("单证");
        }

        private void buttonShowAll_Click(object sender, EventArgs e)
        {

            string str = this.comboBoxOrderCustom.SelectedValue.ToString();
            int nFacID = Convert.ToInt32(str, 10);

            ultraGrid1.DataSource = null;

            DataSet ds = m_bllFacFeeTab.ShowRecvFacFeeRecExNew("FactoryDataTab.FactoryDataTabID = '" + nFacID.ToString() + "'");

            DataSet dsFees = m_bllFacFeeRelationTab.GetListByFac("FactoryDataTab.FactoryDataTabID = '" + nFacID.ToString() + "'");

            DataTable dtNew = new DataTable();

            Fee_HelpClass.MakeColumnNew(dtNew);
            Fee_HelpClass.FillDataTableNew(dtNew, ds, dsFees, null);

            ultraGrid1.DataSource = dtNew;
        }
        private void ShowRecByPaiDanID(int nPaiDanID)
        {
            string str = this.comboBoxOrderCustom.SelectedValue.ToString();
            int nFacID = Convert.ToInt32(str, 10);

            ultraGrid1.DataSource = null;

            DataSet ds = m_bllFacFeeTab.ShowRecvFacFeeRecExNew("PaidanRegisterTab.PaidanRegisterTabID = '" + nPaiDanID.ToString() + "'");

            DataSet dsFees = m_bllFacFeeRelationTab.GetListByFac("FactoryDataTab.FactoryDataTabID = '" + nFacID.ToString() + "'");

            DataTable dtNew = new DataTable();

            Fee_HelpClass.MakeColumnNew(dtNew);
            Fee_HelpClass.FillDataTableNew(dtNew, ds, dsFees, null);

            ultraGrid1.DataSource = dtNew;
        }
        private void buttonRefresh_Click(object sender, EventArgs e)
        {
            string strContract = this.comboBoxContractNo.SelectedValue.ToString();
            int nPaiDanID = Convert.ToInt32(strContract, 10);
            ShowRecByPaiDanID(nPaiDanID);
            
        }
        //清空textbox
        private void ResetAllTextBox(Form form)
        {
            for (int i = 0; i < form.Controls.Count; i++)
            {
                if (form.Controls[i] is TextBox)
                {
                    (form.Controls[i] as TextBox).Text = "";
                }

                if (form.Controls[i] is GroupBox)
                {
                    for (int j = 0; j < form.Controls[i].Controls.Count; j++)
                    {
                        if (form.Controls[i].Controls[j] is TextBox)
                        {
                            (form.Controls[i].Controls[j] as TextBox).Text = "";
                        }
                    }
                }
            }
        }
        private void comboBoxContractNo_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                string str = this.comboBoxContractNo.SelectedValue.ToString();
                int nPaiDanID = Convert.ToInt32(str, 10);
                DataSet ds = m_bllFacFeeTab.ShowRecvFacFeeRecExNew("PaidanRegisterTab.PaidanRegisterTabID = '" + nPaiDanID.ToString() + "'");
                if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                {
                    int nFacFeeTabID = (int)ds.Tables[0].Rows[0]["FacFeeTabID"];
                    DGBY.Model.FacFeeTab modelFacFeeTab = m_bllFacFeeTab.GetModel(nFacFeeTabID);

                    this.textBoxZGZQ.Text = modelFacFeeTab.ZGZQName;
                    this.textBoxDLCWRecv.Text = modelFacFeeTab.ShipDLRecvMoney.ToString();
                    this.textBoxDLCWPay.Text = modelFacFeeTab.ShipDLPayMoney.ToString();
                    this.textBoxHKCWRecv.Text = modelFacFeeTab.ShipHKRecvMoney.ToString();
                    this.textBoxHKCWPay.Text = modelFacFeeTab.ShipHKPayMoney.ToString();
                    this.textBoxHKYSShouldRecv.Text = modelFacFeeTab.TransHKRecvMoney.ToString();
                    this.textBoxHKYSShouldPay.Text = modelFacFeeTab.TransHKPayMoney.ToString();
                    this.textBoxSJJinE.Text = modelFacFeeTab.SJJinE.ToString();
                    this.textBoxShouldPaySJ.Text = modelFacFeeTab.ShouldPaySJJinE.ToString();
                    this.textBoxShouldRecvSJ.Text = modelFacFeeTab.ShouldRecvSJJinE.ToString();
                    this.textBoxShouldPayTransFee.Text = modelFacFeeTab.ShouldPayTransFee.ToString();
                    this.textBoxBGFeeShouldPay.Text = modelFacFeeTab.ShouldPayBGFee.ToString();
                    this.textBoxDZPay.Text = modelFacFeeTab.ShouldPayDZFee.ToString();
                    this.textBoxDHLPay.Text = modelFacFeeTab.ShouldPayDHLFee.ToString();
                    this.textBoxDHLRecv.Text = modelFacFeeTab.ShouldRecvDHLFee.ToString();
                    this.textBoxZXZPay.Text = modelFacFeeTab.ShouldPayZXZFee.ToString();
                    this.textBoxZXZRecv.Text = modelFacFeeTab.ShouldRecvZXZFee.ToString();
                    this.textBoxJCPay.Text = modelFacFeeTab.ShouldPayJCFee.ToString();
                    this.textBoxJCRecv.Text = modelFacFeeTab.ShouldRecvJCFee.ToString();

                    this.comboBoxDLTransComp.SelectedValue = modelFacFeeTab.TCCompDataTabID;
                    this.comboBoxDZComp.SelectedValue = modelFacFeeTab.DZCompDataTabID;
                    this.comboBoxHKTransComp.SelectedValue = modelFacFeeTab.HKTransCompDataTabID;


                }
                else
                {
                    ResetAllTextBox(this);
                }
                ShowRecByPaiDanID(nPaiDanID);

            }
            catch { }
        }

        private void buttonExportToExcel_Click(object sender, EventArgs e)
        {
            DataTable dt = (DataTable)ultraGrid1.DataSource;

            if (dt != null)
            {
                if (dt.Rows.Count > 0)
                {
                    SaveFileDialog sfd = new SaveFileDialog();
                    sfd.Filter = "Excel文件(*.xls)|*.xls";
                    string outFile = "";

                    if (sfd.ShowDialog() == DialogResult.OK)
                    {
                        outFile = sfd.FileName;
                    }
                    else
                    {
                        return;
                    }

                    ultraGridExcelExporter1.Export(ultraGrid1, outFile);
                }
            }
        }

        private void ultraGrid1_InitializeLayout(object sender, Infragistics.Win.UltraWinGrid.InitializeLayoutEventArgs e)
        {
            ClassUltraGridCommonSet.ultraGrid1_InitializeRowFilter(e);
        }
    }
}
